﻿namespace AddressBook.Business.Contact
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	public class EventList : ReadOnlyListBase<EventList, EventInfo>
	{
		#region  "Factory Methods"

		public static EventList EmptyList()
		{
			return new EventList();
		}

		public static EventList GetEventList()
		{
			return DataPortal.Fetch<EventList>();
		}

		private EventList()
		{ /* require use of factory methods */ }

		#endregion

		#region  "Data Access"

		private void DataPortal_Fetch()
		{
			RaiseListChangedEvents = false;

			using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
			{
				List<EventInfo> data = (from e in ctx.DataContext.Events
																 orderby e.Name
																 select new EventInfo(
																	 e.Id,
																	 e.Name,
                                   e.LocationId
																	 )).ToList<EventInfo>();

				this.IsReadOnly = false;

				this.AddRange(data);

				this.IsReadOnly = true;
			}

			RaiseListChangedEvents = true;
		}

		#endregion
	}
}